package com.erp.app.admin.sysParam.beans;

//JRE Classes
import java.sql.Connection;

import com.erp.app.admin.sysParam.dao.SysParamDAO;
import com.erp.app.common.ProjectConstants;
import com.erp.framework.model.dao.ServiceLocator;
import com.erp.framework.model.delegate.SimpleSearchMaintStyleDelegate;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;

public class SysParamDelegate extends SimpleSearchMaintStyleDelegate {
    public SysParamVO getSysParamFor(String paramId) throws Exception {

        Connection conn = null;
        SysParamVO sysParamVO = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SysParamDAO dao = new SysParamDAO();
            sysParamVO = dao.getSysParamFor(paramId, conn); // retrieve only
            // conn.commit();
        } catch (Exception e) {
            System.err.println("Error in SysParamDelegate.getSysParamFor: " + e.toString());
            throw new Exception("Error in SysParamDelegate.getSysParamFor: " + e.toString());
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return sysParamVO;
    }

    public ChildList getAllSysParam() throws Exception {

        Connection conn = null;
        ChildList detail = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SysParamDAO dao = new SysParamDAO();
            detail = dao.getAllSysParam(conn); // retrieve only
            // conn.commit();
        } catch (Exception e) {
            System.err.println("Error in SysParamDelegate.getAllSysParam: " + e.toString());
            throw new Exception("Error in SysParamDelegate.getAllSysParam: " + e.toString());
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return detail;
    }

    public String getSysParamVal(ChildList sysParam, String key) {

        String rslt = "";

        if (sysParam == null)
            return rslt;
        if (CommonUtil.isEmpty(key))
            return rslt;

        for (int i = 0; i < sysParam.size(); i++) {

            SysParamVO spVO = (SysParamVO) sysParam.get(i);
            if (spVO == null)
                continue;

            if (CommonUtil.isEmpty(spVO.getParamId()))
                continue;

            if (spVO.getParamId().trim().equals(key)) {
                rslt = CommonUtil.nullToString(spVO.getParamVal());
                break;
            }
        }
        return rslt;
    }

    public String getSysParamValFor(String paramId) throws Exception {

        Connection conn = null;
        String rslt = "";
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SysParamDAO dao = new SysParamDAO();
            rslt = dao.getSysParamValFor(paramId, conn); // retrieve only
            // conn.commit();
        } catch (Exception e) {
            System.err.println("Error in SysParamDelegate.getSysParamValFor: " + e.toString());
            throw new Exception("Error in SysParamDelegate.getSysParamValFor: " + e.toString());
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return rslt;
    }
}